#!/sbin/openrc-run
# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later

STARTUP_MODE="${STARTUP_MODE:-save}";

extra_started_commands="clear save deactivated"
description="TPM emulator"
command="/usr/bin/tpmd"
my_command_args="-f"
command_background=1
command_user="tss:tss"
pidfile="/var/run/${RC_SVCNAME}.pid"

depend() {
	use logger
	after coldplug
}

start_pre() {
	checkpath -d -m 0775 -o tss /var/run/tpm
	service_set_value STARTUP_MODE "${STARTUP_MODE}"
}

start() {
	command_args="${my_command_args} $(service_get_value STARTUP_MODE)"
	default_start
}

_doit() {
	service_set_value STARTUP_MODE "$1"
	stop
	start
}

clear() {
	_doit clear
}

save() {
	_doit save
}

deactivated() {
	_doit deactivated
}
